﻿using System;

namespace theeg.DependencyAnalysis.CommonAddin.Models
{
    internal class RuleAssert : IAssert
    {
        public void AreEqual(object expected, object actual)
        {
            if (!Equals(expected, actual))
            {
                throw new AssertException("expected: " + expected + " , actual: " + actual);
            }
        }

        public void AreNotEqual(object expected, object actual)
        {
            if (Equals(expected, actual))
            {
                throw new AssertException("expected: " + expected + " , actual: " + actual);
            }
        }

        public void IsTrue(bool value)
        {
            if (!value)
            {
                throw new AssertException();
            }
        }

        public void IsFalse(bool value)
        {
            if (value)
            {
                throw new AssertException();
            }
        }

        public void IsTrue(Func<bool> predicate)
        {
            var result = predicate();
            if (!result)
            {
                throw new AssertException();
            }
        }

        public void IsFalse(Func<bool> predicate)
        {
            var result = predicate();
            if (result)
            {
                throw new AssertException();
            }
        }

        public void Throws<TException>(Action action) where TException : Exception
        {
            try
            {
                action();
                throw new AssertException();
            }
            catch (TException)
            {
            }
        }

        public void DoesNotThrow<TException>(Action action) where TException : Exception
        {
            try
            {
                action();
            }
            catch (TException ex)
            {
                throw new AssertException(ex.Message, ex);
            }
        }
    }
}